package com.app.micaihu.view.main.msgcenter;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.app.AppApplication;
import com.app.micaihu.base.BaseListActivity;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.bean.message.MsgList;
import com.app.micaihu.bean.message.UserMessage;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.view.main.msgcenter.adapter.MsgNoticeAdapter;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.app.micaihu.view.main.msgcenter.MsgCenterListActivity.TITLE_NAME;

/**
 * 消息提醒页面
 * Created by Guo on 2016/12/1.
 */
public class MsgNoticeListActivity extends BaseListActivity<MsgList> {


    private String msgTitleName;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        if(intent != null){
            msgTitleName = intent.getStringExtra(TITLE_NAME);
        }
        super.onCreate(savedInstanceState);
    }
    @Override
    public void onClick(View view) {
        super.onClick(view);
        if(view.getId() == R.id.empty_text_go){

            setResult(RESULT_OK);
            onBackPressed();
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    protected void changeView() {
        super.changeView();
        setTitle(msgTitleName);
        if(mLoadView != null){
            mLoadView.setEmptyGoClickListener(this);
        }

        if (mListView != null) {
            mListView.getRefreshableView().setBackgroundResource(R.color.common_bg_color_5);
            mListView.setBackgroundResource(R.color.common_bg_color_5);
        }
    }

    @Override
    protected void changeData(final boolean isRefresh) {
        Map<String,String> params = new HashMap<>();
        if(UserInforManager.getInstance().isLogin()){
            params.put("uid", UserInforManager.getInstance().getUserInfor().getUid());
        }
        params.put("page",dataPage+"");
        params.put("type","3");
        HttpListener<DataBean<UserMessage>> msgListener = new HttpListener<DataBean<UserMessage>>() {
            @Override
            public void onStart() {
                super.onStart();
                setLoadView(LOADING,null);
            }

            @Override
            public void onError(VolleyError error) {
                setLoadView(NO_NET, AppApplication.getInstance().getString(R.string.neterror));
            }

            @Override
            public void onSuccess(DataBean<UserMessage> response) {
                if(!response.noError()){
                    setLoadView(DATAERROR,getString(R.string.dataerror));
                }else{
                    UserMessage userMessage = response.getData();
                    if(userMessage== null) return;

                    List<MsgList> msgList =
                            userMessage.getMsgList();
                    if(msgList != null && msgList.size() > 0){
                        if(dataList == null){
                            dataList = new ArrayList<MsgList>();
                        }
                        if(isRefresh){
                            //清空数据
                            dataList.clear();
                        }
                        dataList.addAll(msgList);
                        if(mAdapter == null) {
                            mAdapter = new MsgNoticeAdapter(dataList,MsgNoticeListActivity.this);
                            mListView.setAdapter(mAdapter);
                        }else{
                            mAdapter.notifyDataSetChanged();
                        }
                        setLoadView(LOADSUCES,null);
                    }else{


                        if(dataList == null){
                            setLoadView(DATAEMPTY,TypeConstant.EMPTYVIEW_CLOCK,getString(R.string.msg_empty_notice),null,getString(R.string.msg_empty_go));
                        }else{
                            setLoadView(LOADSUCES,getString(R.string.msg_nomore_notice));

                        }
                    }
                }
            }
        };
        addPostCacheRequest(URIContant.MESSAGE_LIST,new TypeToken<DataBean<UserMessage>>(){}.getType(),params,msgListener);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}
